home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / kitten / kitten.c < prev    next >
C/C++ Source or Header  |  1992-03-23  |  3KB  |  163 lines

  1. /* small "cat" program. */
  2.  
  3. #ifndef lint
  4. static char rcsid[] = "$Header: /user5/kupfer/spriteserver/tests/kitten/RCS/kitten.c,v 1.3 92/03/23 15:10:10 kupfer Exp $";
  5. #endif
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <sys.h>
  10. #include <sys/fcntl.h>
  11. #include <sys/types.h>
  12. #include <sys/stat.h>
  13. #include <test.h>
  14. #include <unistd.h>
  15.  
  16. #define USE_MALLOC    1
  17. #define BUF_SIZE    8192
  18. #define USE_CONSOLE    1    /* 0 to use Test_foo to write each line */
  19. #define DO_SHUTDOWN    0    /* 0 to just exit quietly */
  20.  
  21. static void CatFile();
  22. static void DoFile();
  23.  
  24. int
  25. main(argc, argv)
  26.     int argc;
  27.     char *argv[];
  28. {
  29.     int i;
  30.  
  31.     for (i = 1; i < argc; i++) {
  32.     if (argv[i][0] == '-') {
  33.         continue;
  34.     }
  35.     DoFile(argv[i]);
  36.     }
  37.  
  38. #if DO_SHUTDOWN
  39.     (void)Sys_Shutdown(SYS_HALT|SYS_KILL_PROCESSES|SYS_WRITE_BACK, NULL);
  40. #endif
  41.     return 0;
  42. }
  43.  
  44.  
  45. /*
  46.  *----------------------------------------------------------------------
  47.  *
  48.  * DoFile --
  49.  *
  50.  *    Process the named file.
  51.  *
  52.  * Results:
  53.  *    None.
  54.  *
  55.  * Side effects:
  56.  *    If it's a text file, its contents are printed.  For anything else, 
  57.  *    stat information is given for it.
  58.  *
  59.  *----------------------------------------------------------------------
  60.  */
  61.  
  62. static void
  63. DoFile(fileName)
  64.     char *fileName;
  65. {
  66.     struct stat statBuf;
  67.  
  68.     if (stat(fileName, &statBuf) < 0) {
  69.     perror(fileName);
  70.     return;
  71.     }
  72.  
  73.     if ((statBuf.st_mode & S_IFMT) == S_IFREG) {
  74.     CatFile(fileName);
  75.     } else {
  76.     Test_PutMessage("Type: ");
  77.     Test_PutOctal(statBuf.st_mode & S_IFMT);
  78.     Test_PutMessage("\nServer: ");
  79.     Test_PutDecimal(statBuf.st_serverID);
  80.     Test_PutMessage("\nDevice: ");
  81.     Test_PutDecimal(statBuf.st_dev);
  82.     Test_PutMessage("\nI-number: ");
  83.     Test_PutDecimal(statBuf.st_ino);
  84.     Test_PutMessage("\nOwner: ");
  85.     Test_PutDecimal(statBuf.st_uid);
  86.     Test_PutMessage("\n");
  87.     }
  88. }
  89.  
  90.  
  91. /*
  92.  *----------------------------------------------------------------------
  93.  *
  94.  * CatFile --
  95.  *
  96.  *    Print the contents of a file.
  97.  *
  98.  * Results:
  99.  *    None.
  100.  *
  101.  * Side effects:
  102.  *    None.
  103.  *
  104.  *----------------------------------------------------------------------
  105.  */
  106.  
  107. static void
  108. CatFile(fileName)
  109.     char *fileName;
  110. {
  111. #ifdef USE_MALLOC
  112.     char *buf;
  113. #else
  114.     char buf[BUF_SIZE];
  115. #endif
  116.     int file;            /* file descriptor */
  117.     int nChars;            /* number of characters read */
  118. #if USE_CONSOLE
  119.     int cons;            /* console file descriptor */
  120. #endif
  121.  
  122. #ifdef USE_MALLOC
  123.     buf = malloc(BUF_SIZE);
  124.     if (buf == NULL) {
  125.     Test_PutMessage("malloc failed.\n");
  126.     exit(1);
  127.     }
  128. #endif
  129.     file = open(fileName, O_RDONLY);
  130.     if (file < 0) {
  131.     perror(fileName);
  132.     exit(1);
  133.     }
  134. #if USE_CONSOLE
  135.     cons = open("/dev/console", O_WRONLY, 0666);
  136.     if (cons < 0) {
  137.     perror("can't open console");
  138.     exit(1);
  139.     }
  140. #endif /* USE_CONSOLE */
  141.  
  142.     while ((nChars = read(file, buf, BUF_SIZE)) > 0) {
  143. #if USE_CONSOLE
  144.     if (write(cons, buf, nChars) < 0) {
  145.         perror("can't write to console");
  146.         exit(1);
  147.     }
  148. #else
  149.     Test_PutString(buf, nChars);
  150. #endif
  151.     }
  152.     if (nChars < 0) {
  153.     perror("read");
  154.     exit(1);
  155.     }
  156.  
  157.     close(file);
  158. #ifdef USE_MALLOC
  159.     free(buf);
  160. #endif
  161. }
  162.  
  163.